<?php
class PageModel extends Model {
	var $tableName = 'portal_page';	
	
	public function getData($channel,$page){
		$map = $this->parseQuery($channel,$page);
		return $this->where($map)->field('layoutData,widgetData,purview,pageTemp,domain')->find();
	}
	
	public function saveData($channel,$page,$layoutData,$widgetData,$uid,$pageId){
		//dump($uid);
		//dump($pageId);
		
		$map = $this->parseQuery($channel,$page);
		
		$save['layoutData'] = $layoutData;
		$save['widgetData'] = serialize($widgetData);
		$result = $this->where($map)->save($save);
		if($result){
			D('History')->addData($pageId,$uid,$save['layoutData'],$save['widgetData']);
		}
		return $result;
	}
	
	private function parseQuery($channel,$page){
		$result[isNumber($channel) ? 'channelId':'channelDomain'] = $channel;
		$result[isNumber($page) ? 'id':'domain'] = $page;
		return $result;
	}
	
	function getPageInfo($pageId='', $channelId=''){
		if(empty($channelId) && empty($pageId)){
			return '';
		}
		
		//页面信息
		if(!empty($pageId)){
			
			$map[isNumber($pageId) ? 'id':'domain'] = $pageId;
			$map[isNumber($channelId) ? 'channelId':'channelDomain'] = $channelId;
			
			$field = 'id as dataId,channelId,channelDomain,name,pageType,jumpURL,jumpTarget,layoutData,widgetData,domain';
			$data = $this->where($map)->field($field)->find();
			//dump($this->getLastSql());
			$data['pageName'] = $data['name'];
			$data['pagedomain'] = $data['domain'];
			//dump($data);
			unset($data['name']);
			unset($data['domain']);
			
			if(empty($channelId)) $channelId = $data['channelId'];
		}
		
		if(empty($channelId)){
			return $data;
		}
		
		unset($map);
		//频道信息
		$map[isNumber($channelId) ? 'id':'domain'] = $channelId;
		$field = 'title,status,type,temp,id,domain';
		$data1 = D('Channel')->where($map)->field($field)->find();
		$channelId = $data1['id'];
		$data1['html'] = SITE_PATH.'/data/portal/tpl/'.$data1['temp'].'/index.html';
		if(!empty($data)){
			$data = array_merge($data, $data1);
		}
		
		//管理员
		unset($map);
		$map['channelId'] = $channelId;
		$field = 'uid';
		$data2 = D('Purview')->where($map)->field($field)->findall();
		foreach ($data2 as $v){
			$data3[] = $v['uid'];
		}
		
		$data['manager'] = $data3;
		
		return $data;
	}
	
	function initAddPage($channelId, $channelDomain){
		$map['name']      = "首页";
		$map['pageType']  = "leaf";
		$map['domain']    = "index";
		$map['channelId'] = $channelId;
		$map['channelDomain']  = $channelDomain;		
				
		$id = $this->add($map);
		return $id;
	}

	public function addReader($id){
		$id	= intval($id);
		return $this->setInc ( 'readerCount', "id={$id}");
	}
}
?>